package org.stw.bullfrog.ui.managers
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.utils.Dictionary;
	
	import org.stw.bullfrog.core.AppContext;
	import org.stw.bullfrog.ui.events.UIEvent;
	
	public class PopUpManager extends EventDispatcher
	{
		private var _stage:Stage;
		private var _modalSprite:Sprite;
		private var _parent:DisplayObjectContainer;
		private var _windowModalDic:Dictionary;
		private var _windowCenterDic:Dictionary;
		private var _windowPriorityDic:Dictionary;
		private var _windowList:Array;
		public var oldWindowVisible:Boolean = false;
		
		public function PopUpManager(target:IEventDispatcher=null)
		{
			this._stage = AppContext.appStage;
			this.modalSprite = new Sprite();
			this._modalSprite.graphics.beginFill(0, 0.5);
			this._modalSprite.graphics.drawRect(0, 0, 10, 10);
			this._modalSprite.graphics.endFill();
			this._modalSprite.width = this._stage.stageWidth;
			this._modalSprite.height = this._stage.stageHeight;

			this._stage.addEventListener(Event.RESIZE, this.resizeHandler);
		}
		
		private function resizeHandler(event:Event):void
		{
			this._modalSprite.width = this._stage.width;
			this._modalSprite.height = this._stage.height;
		}
		private function onPopUpComplete():void
		{
			this.dispatchEvent(new UIEvent(UIEvent.POPUP_COMPLETE));
		}
		
		public function getWindow():DisplayObject
		{
			if(this._windowList.length>0){
				return this._windowList[this._windowList.length-1];
			}
			return null;
		}
		
		public function getWindowList():Array
		{
			return this._windowList;
		}
		
		public function addPopUp(popView:DisplayObject, isModal:Boolean = false, isCenter:Boolean = true, priority:int = 1):void
		{
			this._windowModalDic[popView] = isModal;
			this._windowCenterDic[popView] = isCenter;
			this._windowPriorityDic[popView] = priority;
			
		}

		public function get parent():DisplayObjectContainer
		{
			return _parent ? _parent : _stage;
		}

		public function set parent(value:DisplayObjectContainer):void
		{
			_parent = value;
		}

	}
}